<template>
  <div class="home">
    <!-- 搜索栏 -->
    <form action="/">
      <van-search
        v-model="value"
        show-action
        placeholder="搜索商品/店铺"
        shape="round"
        background="#f4b2bd"
        @search="onSearch"
        autofocus
      >
        <!-- 搜索框外左侧内容 -->
        <template #left>
          <van-icon name="wap-nav" @click="onCart" style="margin-right: 4px" />
        </template>
      </van-search>
    </form>
    <!-- 轮播图区域 -->
    <div class="myswipe">
      <van-swipe :autoplay="3000" indicator-color="#f4b2bd">
        <van-swipe-item v-for="(image, index) in images" :key="index">
          <img v-lazy="image.image" class="img" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!-- 主页分类区域 -->
    <div class="classify">
      <van-grid column-num="5" :border="false" clickable icon-size="40px">
        <van-grid-item
          v-for="(item, index) in navList.slice(0, 10)"
          :key="index"
          :icon="item.imageUrl"
          :text="item.name"
          :to="`/cate/sub?id=${item.id}`"
        >
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 限时购区域 -->
    <div class="timeLimit">
      <van-cell-group inset>
        <van-cell :border="false">
          <template #title>
            <h3>限时购</h3>
            <!-- 倒计时 -->
            <div class="timeout">
              <span>距结束</span>
              <van-count-down :time="time">
                <template #default="timeData">
                  <span class="block">{{ "0" + timeData.hours }}</span>
                  <span class="colon">:</span>
                  <span class="block">{{ timeData.minutes }}</span>
                  <span class="colon">:</span>
                  <span class="block">{{ timeData.seconds }}</span>
                </template>
              </van-count-down>
            </div>
          </template>
          <template #extra>
            <span @click="more">更多 <van-icon name="arrow" /></span>
          </template>
        </van-cell>
        <van-cell class="contain">
          <template #default>
            <ul>
              <li
                v-for="(item, index) in itemList.slice(0, 5)"
                :key="index"
                @click="enterDetails(item.id)"
              >
                <img :src="item.image" />
                <div class="price">{{ "¥" + item.price }}</div>
              </li>
            </ul>
          </template>
        </van-cell>
      </van-cell-group>
    </div>
    <!-- 商品列表区域 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-card
        v-for="(item, index) in itemList"
        :key="index"
        :price="item.price"
        :desc="item.title"
        :title="item.qunTitle"
        :thumb="item.image"
        class="card"
      >
        <template #tags>
          <van-tag plain type="danger" class="box">{{
            item.expireMsg
          }}</van-tag>
          <van-tag plain type="danger">{{ item.rankingTag }}</van-tag>
        </template>
        <template #footer>
          <van-button size="mini" @click="enterDetails(item.id)"
            >详情</van-button
          >
          <van-button size="mini" @click="purchase">立即购买</van-button>
        </template>
      </van-card>
    </van-list>
    <tabbar></tabbar>
  </div>
</template>

<script>
import { getBanner, getItems, getCateItems } from "../../api/index";
// 引入想要使用的组件
import tabbar from "../../components//myTabbar/index.vue";

import Vue from "vue";
import { Lazyload } from "vant";
import { Toast } from "vant";

Vue.use(Lazyload);

export default {
  data() {
    return {
      images: [],
      value: "",
      itemList: [],
      // 首页分类数据
      navList: [],
      loading: false,
      finished: false,
      nextIndex: 1, //请求下一页数据的变量
      // 倒计时
      time: 30 * 60 * 60 * 1000,
    };
  },
  components: {
    tabbar,
  },
  mounted() {
    this.getBanner();
    this.getItems();
    this.getCateItems();
  },
  methods: {
    // 点击进入详情页
    enterDetails(id) {
      // console.log("点击进去详情的ID",id);
      this.$router.push("/detail/=" + id);
    },
    getBanner() {
      getBanner().then((res) => {
        // console.log(res, "getbanner的数据");
        // console.log(res.data.data.topList, "imgs");
        this.images = res.data.data.items.list;
      });
    },
    onSearch(val) {
      Toast(val);
    },
    onCart() {
      this.$router.push("/cate");
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      getItems({ start: this.nextIndex }).then((res) => {
        // console.log(res, "上拉加载getItems的数据");
        // console.log(res.data.data.list, "上拉加载list的数据");
        // this.itemList = res.data.data.list;
        // 加载状态结束
        this.loading = false;
        // 数据的累加
        this.itemList = [...this.itemList, ...res.data.data.list];
        // 把数据变活
        this.nextIndex = res.data.data.nextIndex;
      });
    },
    // 获取首页所有的商品
    getItems() {
      getItems({ start: 0 }).then((res) => {
        // console.log(res, "getItems的数据");
        // console.log(res.data.data.list, "list的数据");
        this.itemList = res.data.data.list;
      });
    },
    // 获取分类列表数据
    getCateItems() {
      getCateItems().then((res) => {
        // console.log(res);
        this.navList = res.data.data.list;
      });
    },
    // 点击更多
    more() {
      Toast({
        message: "没有了哦",
        icon: "like",
      });
    },
    purchase() {
      this.$toast.success('已购买')
    }
  },
};
</script>

<style scoped>
h3 {
  display: inline-block;
  margin: 0;
  padding: 0;
  margin-right: 5px;
}
.home {
  font-size: 20px;
  color: #ffffff;
  background-color: #f1f3f5;
}
.myswipe {
  height: 200px;
  margin-bottom: 10px;
  background: linear-gradient(#f8afaf, #9198e5);
  border-radius: 0% 0% 20% 70% / 30% 30% 40% 40%;
}
.van-swipe {
  height: 200px;
  margin: 0 13px;
  border-radius: 10px;
}

.img {
  width: 100%;
  height: 100%;
}
.card {
  margin: 10px 10px;
  background-color: #fff;
  border-radius: 10px;
}
>>>.van-grid-item__content {
  padding: 1px 6px;
}
/* 限时购区域 */
.timeLimit {
  margin: 10px 0;
  /* height: 347px; */
}

/* 倒计时区域 */
.colon {
  display: inline-block;
  margin: 0 4px;
  color: #bf2a34;
}

.block {
  display: inline-block;
  width: 16px;
  color: #fff;
  font-size: 12px;
  text-align: center;
  background-color: #bf2a34;
  border-radius: 3px;
}

.van-count-down {
  display: inline-block;
}

.timeout {
  display: inline-block;
}

.timeout > span {
  font-size: 8px;
  color: #666666;
}
/* 限时商品 */
.contain > div > ul > li {
  display: inline-block;
  margin: 0 5px;
}
.contain > div > ul > li > img {
  width: 50px;
  height: 50px;
  padding: 0 5px;
}
.price {
  color: #bf2a34;
  font-weight: 600;
}
</style>
